package com.fsq.xmfsubdomain;

import java.net.InetAddress;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;

import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

public class NetcraftSpider extends Task<String> {

	private CountDownLatch countDownLatch;
	private LinkedBlockingQueue<String> queue;
	private Set<InetAddress> blacklist;
	private TreeView<String> id_treeview;
	private TableView<Row> id_tableview;
	private String domain;
	private Label id_status;
	private Set<String> ipList;
	private Set<Record> allRecordSet;
	private TreeItem<String> tiLevel2;

	public NetcraftSpider(CountDownLatch countDownLatch, LinkedBlockingQueue<String> queue, Set<InetAddress> blacklist,
						  TreeView<String> id_treeview, TableView<Row> id_tableview, String domain, Label id_status,
						  Set<String> ipList, Set<Record> allRecordSet, TreeItem<String> tiLevel2) {
		super();
		this.countDownLatch = countDownLatch;
		this.queue = queue;
		this.blacklist = blacklist;
		this.id_treeview = id_treeview;
		this.id_tableview = id_tableview;
		this.domain = domain;
		this.id_status = id_status;
		this.ipList = ipList;
		this.allRecordSet = allRecordSet;
		this.tiLevel2 = tiLevel2;
	}

	@Override
	protected String call() {

		try {
			ObservableList<Row> items = id_tableview.getItems();
			String url;

			String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36";
			CloseableHttpClient httpclient = HttpClients.custom().setUserAgent(userAgent).build();
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000)
					.setConnectionRequestTimeout(3000).setSocketTimeout(3000).build();

			while ((url = queue.poll(3, TimeUnit.SECONDS)) != null) {
				String strurl = url;
				Platform.runLater(new Runnable() {

					@Override
					public void run() {
						id_status.setText("正在爬取" + strurl);

					}
				});
				System.out.println("正在爬取" + url);

				HttpGet httpGet = new HttpGet(url);
				httpGet.setConfig(requestConfig);
				CloseableHttpResponse response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String pagedoc = EntityUtils.toString(entity, "utf-8");
				Document doc = Jsoup.parse(pagedoc);
				Elements aElements = doc.getElementsByAttributeValue("rel", "nofollow");

				String domainname;
				for (Element element : aElements) {
					domainname = element.text();
					System.out.println("netcraft找到域名" + domainname);

					Record[] records = new Lookup(domainname).run();
					for (Record record : records) {
						if (allRecordSet.add(record)) {
							// 添加成功说明之前没有收录
							ARecord ar = (ARecord) record;
							InetAddress address = ar.getAddress();
							String ip = address.getHostAddress();
							ipList.add(ip);
							Row row = new Row(domain, domainname, ip, "Netcraft", "2", "A记录");
							TreeItem<String> tiBaidu = new TreeItem<String>(domainname);
							Platform.runLater(new Runnable() {

								@Override
								public void run() {
									tiLevel2.getChildren().add(tiBaidu);
									items.add(row);

								}
							});

						}
					}

				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			countDownLatch.countDown();
			System.out.println("剩余Netcraft爬虫线程数：" + countDownLatch.getCount());
		}
		return null;
	}

}
